import { ECarDir, ECarType } from "./Const";
import GameUI from "./GameUI";
import GlobalData from "./GlobalData";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Carport extends cc.Component {

    @property({ type: [cc.SpriteFrame], tooltip: "车库图片" })
    private sprites: cc.SpriteFrame[] = [];

    @property({ type: cc.Sprite, tooltip: "车库图" })
    private sprite: cc.Sprite = null;

    @property({ type: cc.Sprite, tooltip: "车阴影" })
    private shadow: cc.Sprite = null;

    @property({ type: cc.Label, tooltip: "剩余数量" })
    private lab: cc.Label = null;

    @property({ type: cc.Node, tooltip: "纵向区域" })
    private rect1: cc.Node = null;

    @property({ type: cc.Node, tooltip: "横向区域" })
    private rect2: cc.Node = null;

    @property({
        tooltip: "车库朝向", step: 1, min: 0, max: 3, visible() {
            this.updateCar();
            return true;
        }
    })
    private _dir: ECarDir = ECarDir.Up;
    public get dir() {
        return this._dir;
    }

    @property({
        tooltip: "车库大小", step: 1, min: 0, max: 2, visible() {
            this.updateCar();
            return true;
        }
    })
    private _type: ECarType = ECarType.Small;
    public get type() {
        return this._type;
    }

    @property({
        tooltip: "剩余数量", step: 1, min: 0, max: 30, visible() {
            this.updateNum();
            return true;
        }
    })
    private _num: number = 0;
    public get num() {
        return this._num;
    }
    public set num(value: number) {
        this._num = value;
        this.updateNum();
    }

    protected onLoad() {
        this.initArea();
        this.checkIntersect();
    }

    // 设置车库碰撞区域
    private initArea() {
        this.rect1.width = this.node.width;
        this.rect1.height = this.node.height + 300;
        this.rect2.width = this.node.width + 300;
        this.rect2.height = this.node.height;
        // console.log("sss:", this.rect1.width, this.rect1.height, this.rect2.width, this.rect2.height);
    }

    private updateCar() {
        const dir = this._dir === 3 ? 1 : this._dir;
        this.sprite.spriteFrame = this.sprites[dir * 3 + this._type];
        this.shadow.spriteFrame = this.sprites[dir * 3 + this._type];
        this.shadow.node.scaleX = this._dir === 3 ? -1 : 1;
        this.sprite.node.scaleX = this._dir === 3 ? -1 : 1;
        this.node.width = this.sprite.node.width;
        this.node.height = this.sprite.node.height;
        this.initArea();
    }

    private updateNum() {
        this.lab.string = `${this._num}`;
    }

    public isHad() {
        return this._num > 0;
    }

    public checkIntersect() {
        const cars = GameUI.instance.cars.children;
        for (let i = 0; i < cars.length; i++) {
            if (cars[i] !== this.node && cars[i].name !== 'rect') {
                switch (this._dir) {
                    case ECarDir.Up:
                        {
                            if (cars[i].y > this.node.y && GlobalData.intersect(cars[i], this.rect1)) {
                                // console.log("前挡");
                                return true;
                            }
                        }
                        break;
                    case ECarDir.Down:
                        {
                            if (cars[i].y < this.node.y && GlobalData.intersect(cars[i], this.rect1)) {
                                // console.log("下挡");
                                return true;
                            }
                        }
                        break;
                    case ECarDir.Left:
                        {
                            if (cars[i].x < this.node.x && GlobalData.intersect(cars[i], this.rect2)) {
                                // console.log("左挡");
                                return true;
                            }
                        }
                        break;
                    case ECarDir.Right:
                        {
                            if (cars[i].x > this.node.x && GlobalData.intersect(cars[i], this.rect2)) {
                                // console.log("右挡");
                                return true;
                            }
                        }
                        break;
                }
            }
        }
        return false;
    }
}